package bb.lanxing.lib.devices.ble.device;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.base.SimpleBluetoothGattCallbackCompat;
import bb.lanxing.lib.devices.core.ble.AbsBleDevice;
import bb.lanxing.lib.devices.core.provider.DataNotifier;
import java.util.UUID;
import android.util.Log;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import bb.lanxing.lib.devices.ble.BLEAttributes;

public class StandardIndoorbikeDevice extends AbsBleDevice {
    final static private String TAG = "StandardIndoorbikeDevice";
    final static private boolean DebugFTMS = false;

    public StandardIndoorbikeDevice(Context context, SmartDevice smartDevice) {
        super(smartDevice);
        init(context, smartDevice.getAddress(), new IndoorbikeBluetoothGattCallback(this));
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onServicesDiscovered() {
        super.onServicesDiscovered();
        BluetoothGattCharacteristic characteristic = mBluetoothGatt.getService(UUID.fromString(BLEAttributes.BLE_FTMS_SERVICE)).getCharacteristic(UUID.fromString(BLEAttributes.BLE_FTMS_CHAR_INDOOR_BIKE_DATA));
        mBluetoothGatt.readCharacteristic(characteristic);
        setCharacteristicNotification(characteristic, true);

        if(DebugFTMS){
            //do nothing,just debug
        }else{
            writeDeviceName00();
            writeDeviceName07();
        }
    }
    private static class IndoorbikeBluetoothGattCallback extends SimpleBluetoothGattCallbackCompat<StandardIndoorbikeDevice> {
        IndoorbikeBluetoothGattCallback(StandardIndoorbikeDevice standardIndoorbikeDevice) {
            super(standardIndoorbikeDevice);
        }

        public int unsignedByteToInt(byte b) {
            return b & 0xFF;
        }

        public int unsignedBytesToInt24(byte b0, byte b1, byte b2) {
            return (unsignedByteToInt(b0) + (unsignedByteToInt(b1) << 8))
                    + (unsignedByteToInt(b2) << 16) ;
        }
        @Override
        public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            int intValueFlag1byte = 0;
            int intValueFlag2byte = 0;
            double doubleValueSpeed = 0;
            int intValueCadence = 0;
            int intValuePower = 0;
            int intValueResistanceLevel = 0;
            int intValueEnergy = 0;
            double  doubleValueTotalDistance = 0;
            int intValueHeartRate = 0 ;

            super.onCharacteristicChanged(bluetoothGatt, bluetoothGattCharacteristic);
            UUID uuid = bluetoothGattCharacteristic.getUuid();
            int type = ((StandardIndoorbikeDevice) this.mClientRef).getType();

            if (UUID.fromString(BLEAttributes.BLE_FTMS_CHAR_INDOOR_BIKE_DATA).equals(uuid)) {
                intValueFlag1byte = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
                intValueFlag2byte = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                if(DebugFTMS){
                    intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                    doubleValueSpeed = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1)*100)/360).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    intValueCadence = (int)bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1)/2;
                    intValueResistanceLevel = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                    intValueEnergy = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                    doubleValueTotalDistance = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1))*1000).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    intValueHeartRate = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
                }else if ((intValueFlag1byte == 116) && (intValueFlag2byte == 11)) {  //0x74 0x0B Chongqing LD indoor bike,  FS-LD CX50
                    doubleValueSpeed = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 2))/360).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    intValueCadence = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 4)/2);
                    intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 11);
                    intValueResistanceLevel = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 9);
                    intValueEnergy = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 13);
                    byte[] value = bluetoothGattCharacteristic.getValue();
                    doubleValueTotalDistance = new BigDecimal((float)(unsignedBytesToInt24(value[6],value[7],value[8]))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    intValueHeartRate = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 18);
                }else if ((intValueFlag1byte == 118) && (intValueFlag2byte == 11))  { //0x76 0x0B  Decathlon indoor bike, Domyos-Bike-XXXX
                    doubleValueSpeed = new BigDecimal((float)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 2))/360).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    intValueCadence = (int)(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 6)/2);
                    intValuePower = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 13);
                    intValueResistanceLevel = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 11);
                    intValueEnergy = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 15);
                    byte[] value = bluetoothGattCharacteristic.getValue();
                    doubleValueTotalDistance = new BigDecimal((float)(unsignedBytesToInt24(value[8],value[9],value[10]))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    intValueHeartRate = bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 20);
                }
                DataNotifier.notifyIndoorbikeData(type, ((StandardIndoorbikeDevice) this.mClientRef).getAddress(), doubleValueSpeed,intValueCadence,intValuePower,intValueResistanceLevel,intValueEnergy,doubleValueTotalDistance,intValueHeartRate);
            }
        }
    }
}